package com.intexh.speedandroid.module.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.intexh.speedandroid.R;
import com.intexh.speedandroid.base.BaseFragment;
import com.intexh.speedandroid.helper.LoginStatusEvent;
import com.intexh.speedandroid.helper.UserBean;
import com.intexh.speedandroid.helper.UserHelper;
import com.intexh.speedandroid.net.Apis;
import com.intexh.speedandroid.net.NetworkManager;
import com.intexh.speedandroid.net.interfaces.OnRequestCallBack;
import com.intexh.speedandroid.utils.GsonUtils;
import com.intexh.speedandroid.utils.Settings;
import com.intexh.speedandroid.utils.ValidateUtils;
import com.intexh.speedandroid.utils.glide.GlideHelper;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/2/2 0002.
 */

public class MineFragment extends BaseFragment {
    @BindView(R.id.setting_tv)
    TextView settingTv;
    Unbinder unbinder;
    @BindView(R.id.avatar_iv)
    ImageView avatarIv;
    @BindView(R.id.user_name_tv)
    TextView userNameTv;
    @BindView(R.id.level_tv)
    TextView levelTv;
    @BindView(R.id.order_llt)
    LinearLayout orderLlt;
    @BindView(R.id.feed_back_tv)
    TextView feedBackTv;
    @BindView(R.id.share_tv)
    TextView shareTv;
    @BindView(R.id.about_us_tv)
    TextView aboutUsTv;
    private UserBean userInfoBean;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        EventBus.getDefault().register(this);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    protected void initView() {
        if(UserHelper.isLogin()){
            userInfoBean = UserHelper.getUser();
            if(ValidateUtils.isValidate(userInfoBean)){
                refreshUI(userInfoBean);
            }
            loadUserData();
        }else{
            userNameTv.setText("请登录");
            GlideHelper.INSTANCE.loadCircleImage(avatarIv,R.mipmap.ic_launcher);
            levelTv.setVisibility(View.GONE);
        }
    }

    /**
     * 项目中维护个人信息 只用一个UserBean ,调用个人信息 后同步本地个人信息
     */
    private void loadUserData() {
        Map<String,String> params= new HashMap<>();
        NetworkManager.INSTANCE.post(Apis.memberDataInfo, params, new OnRequestCallBack() {
            @Override
            public void onOk(String response) {
                if(userNameTv==null)return;
                userInfoBean = GsonUtils.jsonToBean(response, UserBean.class);
                UserHelper.syncCurrentUserInfo(userInfoBean);   //同步个人信息
                refreshUI(userInfoBean);
            }

            @Override
            public void onError(int code,String errorMessage) {

            }
        });
    }

    private void refreshUI(UserBean userInfoBean) {
//        userNameTv.setText(userInfoBean.getMember_name());
//        levelTv.setText("等级："+userInfoBean.getMember_grade());
//        GlideHelper.INSTANCE.loadCircleImage(avatarIv,userInfoBean.getMember_avatar());
    }

    @Override
    public void onResume() {
        super.onResume();
        if (settingTv != null) {
            initView();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LoginStatusEvent event) {
        initView();
    }


    @OnClick({R.id.order_llt, R.id.user_name_tv,R.id.avatar_iv,R.id.message_iv,R.id.feed_back_tv,R.id.level_tv, R.id.share_tv, R.id.about_us_tv, R.id.setting_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.user_name_tv:
                break;
            case R.id.message_iv:
                break;
            case R.id.avatar_iv:
                break;
            case R.id.order_llt:
                if(UserHelper.checkGoLogin(mContext))return;
                break;
            case R.id.feed_back_tv:
                startActivity(FeedBackActivity.class);
                break;
            case R.id.level_tv:
                if(userInfoBean==null){
                    showToast("正在加载会员信息...");
                    initView();
                    return;
                }
                startActivity(LevelActivity.class);
                break;
            case R.id.share_tv:
//                WebViewActivity.startActivity(mContext, WebApis.download);
                break;
            case R.id.about_us_tv:
//                WebViewActivity.startActivity(mContext, WebApis.about);
                break;
            case R.id.setting_tv:
                startActivityForResult(SettingActivity.class,100);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode== Activity.RESULT_OK && requestCode==100){
            initView();
        }
    }
}
